package com.wnnight.wys.web.car;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnnight.wys.entity.car.Park;
import com.wnnight.wys.groupInterface.AddGroup;
import com.wnnight.wys.security.JwtUtils;
import com.wnnight.wys.service.car.ParkService;
import com.wnnight.wys.vo.ResultVo;
import com.wnnight.wys.vo.car.CarDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * @ClassName: ParkController
 * @Description: Park控制器
 * @author: CHX42
 * @date: 2022/8/28-18:42
 * @version: v1.0
 */
@RestController
@RequestMapping("/api/v1/car/park")
@Slf4j
public class ParkController {
    @Autowired
    private ParkService parkService;

    @PostMapping("/list")
    @PreAuthorize("hasAuthority('car:park:list')")
    public ResultVo<?> list(@RequestBody CarDataVo<Park, Park> parkCarVo) {
        log.debug("车位列表");
        QueryWrapper<Park> parkWrapper = new QueryWrapper<>();
        String likeName = parkCarVo.getData().getName();
        String likePhone = parkCarVo.getData().getPhone();
        String likeParkNum = parkCarVo.getData().getParkNum();
        String status = parkCarVo.getData().getStatus();
        parkWrapper.like(StringUtils.hasText(likeName), "wys_landlord.name", likeName);
        parkWrapper.like(StringUtils.hasText(likePhone), "wys_landlord.phone", likePhone);
        parkWrapper.like(StringUtils.hasText(likeParkNum), "wys_park.park_num", likeParkNum);
        parkWrapper.eq(StringUtils.hasText(status), "wys_park.status", status);
        parkWrapper.orderByAsc("wys_park.park_num");
        Page<Park> backPage = parkService.getpage(new Page<>(parkCarVo.getPagePlus().getCurrent(), parkCarVo.getPagePlus().getSize()), parkWrapper);
        return new ResultVo<>(backPage);
    }

    @PostMapping("/getParkInfo")
    @PreAuthorize("hasAuthority('car:park:info')")
    public ResultVo<?> getParkInfo(@RequestBody Park park) {
        log.debug("车位详情");
        Integer id = park.getId();
        QueryWrapper<Park> parkWrapper = new QueryWrapper<>();
        parkWrapper.eq("wys_park.id", id);
        Page<Park> backPage = parkService.getpage(new Page<>(1, 10), parkWrapper);
        return new ResultVo<>(backPage.getRecords().get(0));
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('car:park:udp')")
    public ResultVo<?> update(@RequestBody @Validated({AddGroup.class}) Park park) {
        log.debug("车位修改");
        UpdateWrapper<Park> parkWrapper = new UpdateWrapper<>();
        Integer id = park.getId();
        park.setParkNum(null);
        park.setType(null);
        if ("1".equals(park.getStatus())) {
            parkWrapper.set("landlord_id",null);
        }
        parkWrapper.eq("wys_park.id",id);
        parkService.update(park,parkWrapper);
        return ResultVo.SUCCESS;
    }


    @PostMapping("/getbylandlordid")
//    @PreAuthorize("hasAuthority('car:car:landcar')")
    public ResultVo<?> getByLandlordId(HttpServletRequest request) {
        log.debug("房东车辆列表");
        String jwt = request.getHeader("jwt");
        Integer id =(Integer) JwtUtils.getInfo(jwt, "id");
        return new ResultVo<>(parkService.getByLandlordId(id));
    }
}
